﻿
(function () {
    var defaultToPixel = CKEDITOR.tools.cssLength;
    // Whole-positive-integer validator.
    function validatorNum(msg) {
        return function () {
            var value = this.getValue(),
				pass = !!(CKEDITOR.dialog.validate.integer()(value) && value > 0);

            if (!pass) {
                alert(msg);
                this.select();
            }

            return pass;
        };
    }

    var commitValue = function (data) {
        var id = this.id;
        if (!data.info)
            data.info = {};
        data.info[id] = this.getValue();
    };


    CKEDITOR.dialog.add('3linetable', function (editor) {
        var makeElement = function (name) {
            return new CKEDITOR.dom.element(name, editor.document);
        };

        return {
            title: '插入三线表',
            minWidth: 500,
            minHeight: 150,
            contents:
			[
				{
				    id: 'info',
				    label: '测试',
				    title: '测试1',
				    elements:
					[
						{
						    type: 'text',
						    id: 'txtRows',
						    'default': 5,
						    label: '行数',
						    required: true,
						    controlStyle: 'width:5em',
						    validate: validatorNum('指定的行数必须大于零'),
						    commit: commitValue
						},
						{
						    type: 'text',
						    id: 'txtCols',
						    'default': 3,
						    label: '列数',
						    required: true,
						    controlStyle: 'width:5em',
						    validate: validatorNum('指定的列数必须大于零'),
						    commit: commitValue
						},
                        {
                            type: 'text',
                            id: 'txtWidth',
                            controlStyle: 'width:5em',
                            label: '宽度',
                            title: '该字段必须为合适的CSS长度值，包括单位(px, %, in, cm, mm, em, ex, pt 或 pc)',
                            'default': '',
                            getValue: defaultToPixel,
                            validate: CKEDITOR.dialog.validate.cssLength(editor.lang.common.invalidCssLength.replace('%1', editor.lang.common.width)),
                            commit: commitValue
                        }
					]
				}
			],
            onOk: function () {
                var table = makeElement('table'),
					me = this,
					data = {};

                this.commitContent(data, table);
                table.setAttributes({
                    'border': "0", 'cellpadding': "0", 'cellspacing': "0"
                });
                table.setStyles({
                    'border-style': 'solid', 'border-color': 'black', 'border-width': '1px 0 1px 0'//, 'min-width': '300px'
                });
                table.addClass("cke_show_border");
                if (data.info) {
                    var info = data.info;
                    var tbody = table.append(makeElement('tbody')),
                            rows = parseInt(info.txtRows, 10) || 0,
                            cols = parseInt(info.txtCols, 10) || 0;

                    for (var i = 0; i < rows; i++) {
                        var row = tbody.append(makeElement('tr'));

                        for (var j = 0; j < cols; j++) {
                            var cell = row.append(makeElement('td'));
                            if (i == 0) {
                                cell.setStyle("border-bottom", "1px solid black");
                            }
                            if (!CKEDITOR.env.ie)
                                cell.append(makeElement('br'));
                        }
                    }

                    // Set the width and height.
                    info.txtWidth ? table.setStyle('width', info.txtWidth) : table.removeStyle('width');

                    editor.insertElement(table);
                    // Override the default cursor position after insertElement to place
                    // cursor inside the first cell (#7959), IE needs a while.
                    setTimeout(function () {
                        var firstCell = new CKEDITOR.dom.element(table.$.rows[0].cells[0]);
                        var range = new CKEDITOR.dom.range(editor.document);
                        range.moveToPosition(firstCell, CKEDITOR.POSITION_AFTER_START);
                        range.select(1);
                    }, 0);
                }
            }
        };
    });
})();
